package myky.com.doctorapp.ui

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.view.View
import com.chad.library.adapter.base.BaseQuickAdapter
import com.scwang.smartrefresh.layout.api.RefreshLayout
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener
import kotlinx.android.synthetic.main.activity_list.*
import kotlinx.android.synthetic.main.public_common_title.*
import myky.com.doctorapp.R
import myky.com.doctorapp.adapter.ListAdapter
import myky.com.doctorapp.entity.DiseaseEntity
import myky.com.doctorapp.base.BaseActivity
import myky.com.doctorapp.contract.ListActivityContract
import myky.com.doctorapp.presenter.ListActivityPresenter
import myky.com.doctorapp.utils.Constants

/**
 * Description:简单的liststring选择列表
 * @author wuyangwuyang
 * Created by 2019/2/14 on 15:46
 */
class ListActivity:BaseActivity<ListActivityPresenter, DiseaseEntity.DataBean>(),ListActivityContract.View ,View.OnClickListener,OnRefreshLoadMoreListener, BaseQuickAdapter.OnItemChildClickListener{



    var current:Int = 1
    private var page:Int=1
    private var doctorId:Int=0
    private var hasMore:Boolean=true
    private lateinit var listAdapter: ListAdapter
    private lateinit var listString: MutableList<String>
    lateinit var diseaseList:MutableList<DiseaseEntity.DataBean.DiseaseListBean>
    lateinit var type:String
    override fun getPresenter(): ListActivityPresenter = ListActivityPresenter()
    override fun reloadActivity() {

    }
    override fun onClick(view:View?) {

        when(view?.id){

            R.id.backLayout->finish()
        }
    }
    override fun onItemChildClick(adapter: BaseQuickAdapter<*, *>?, view: View?, position: Int) {

        var string=listAdapter.data[position]
        var index:Int=position
        if (type == Constants.DISEASETYPE){
            //如果是疾病类型进来的获取疾病类型id传出
            if (diseaseList!=null){
            index=diseaseList[position].diseaseid
            }
        }
        val intent=Intent()
        val bundle=Bundle()
        bundle.putString(Constants.RESULTDATE,string)
        bundle.putInt(type,index)
        intent.putExtras(bundle)
        setResult(Activity.RESULT_OK,intent)
        finish()

    }
    override fun injectTarget(): View =findViewById(R.id.listactivity)

    override fun initData() {
        var bundle=intent.extras
        type=bundle.getString(Constants.INTENTTYPE)
        doctorId=bundle.getInt(Constants.DOCTORID)

        when(type){

            Constants.DISEASETYPE->{
                conmonTitleTextView.text=resources.getString(R.string.RemoteSubmit_nicktype)
                mPresenter.pDiseaseList(doctorId,current,1,10)

            }

            Constants.HEALTHTYPE-> {
                conmonTitleTextView.text=resources.getString(R.string.RemoteSubmit_health)
                list_refresh.setEnableLoadMore(false)
                list_refresh.setEnableRefresh(false)
            }
            Constants.VISITTYPE-> {
                conmonTitleTextView.text=resources.getString(R.string.RemoteSubmit_visit)
                list_refresh.setEnableLoadMore(false)
                list_refresh.setEnableRefresh(false)
                listString.addAll(resources.getStringArray(R.array.remote_freq))
                listAdapter.notifyDataSetChanged()
            }
            Constants.ISKONOWTYPE-> {
                conmonTitleTextView.text=resources.getString(R.string.RemoteSubmit_iskonow)
                list_refresh.setEnableLoadMore(false)
                list_refresh.setEnableRefresh(false)
                listString.addAll(resources.getStringArray(R.array.remote_known))
                listAdapter.notifyDataSetChanged()
            }

        }

    }
    override fun onLoadMore(refreshLayout: RefreshLayout) {
        if (hasMore){
            current++
            mPresenter.pDiseaseList(doctorId, current, 1, 10)
        }else {
            refreshLayout.finishLoadMoreWithNoMoreData()
            refreshLayout.finishLoadMore(10)
        }
    }

    override fun onRefresh(refreshLayout: RefreshLayout) {
        hasMore=true
        current=1
        listString.clear()
        mPresenter.pDiseaseList(doctorId,current,1,10)
    }

    override fun initView() {

        listString=ArrayList()
        listAdapter= ListAdapter(listString)
        list_recycler.layoutManager=LinearLayoutManager(this)
        list_recycler.adapter=listAdapter
        backLayout.setOnClickListener(this)
        list_refresh.setOnRefreshLoadMoreListener(this)
        list_refresh.setEnableLoadMoreWhenContentNotFull(false)
        listAdapter.onItemChildClickListener = this

    }


    override fun getContenView(): Int = R.layout.activity_list

    override fun showDataSuccess(datas: DiseaseEntity.DataBean) {
        var list :MutableList<String>?=ArrayList()
        diseaseList = datas.diseaseList
        //不够10条显示没有更多数据
        if (diseaseList != null) {
        if (diseaseList.size<10){hasMore=false}
            for (item in diseaseList){
                list?.add(item.diseasename)
            }
            if (list != null) {
                listAdapter.addData(list)
                list_refresh.finishLoadMore(10)
                list_refresh.finishRefresh(10)
            }
        }

    }
}